package com.project.sianet.data;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.project.sianet.R;
import com.project.sianet.entidades.*;
import com.project.sianet.util.*;

public class AlumnoArrayAdapter extends ArrayAdapter<Alumno> {

	private static final String tag = "AlumnoArrayAdapter";
	private Activity activity;
    private Context context;
    private ImageView alumnoIcon;
    private TextView alumnoLogin;
    private TextView alumnoNombres;
    private TextView alumnoApellidos;
    private TextView alumnoGrado;
    private List<Alumno> alumnos = new ArrayList<Alumno>();
    private SharedPreferences prefs;
    private String host;
    
    public AlumnoArrayAdapter(Activity activity, int textViewResourceId,
            List<Alumno> objects) {
        super(activity, textViewResourceId, objects);
        this.activity = activity;
        this.context = activity.getApplicationContext();
        this.alumnos = objects;
        prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
        host = context.getString(R.string.pref_host_default) + prefs.getString("ColegioPF", "");
    }
    
    public int getCount() {
        return this.alumnos.size();
    }
 
    public Alumno getItem(int index) {
        return this.alumnos.get(index);
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            // ROW INFLATION
            Log.d(tag, "Starting XML Row Inflation ... ");
            LayoutInflater inflater = (LayoutInflater) this.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.mishijos_list, parent, false);
            Log.d(tag, "Successfully completed XML Row Inflation!");
        }
 
        Alumno alu = getItem(position);
         
        alumnoIcon = (ImageView) row.findViewById(R.id.mishijos_icon);
        alumnoApellidos = (TextView) row.findViewById(R.id.mishijos_apellidos);
        alumnoNombres = (TextView) row.findViewById(R.id.mishijos_nombres);
        alumnoGrado = (TextView) row.findViewById(R.id.mishijos_grado);
        alumnoLogin = (TextView) row.findViewById(R.id.mishijos_login);
 
        alumnoApellidos.setText(alu.getApellidos());
        alumnoNombres.setText(alu.getNombres());
        alumnoGrado.setText(alu.getGrado());
        alumnoLogin.setText(alu.getLogin());
        
        ImageLoader imgLoader = new ImageLoader(activity);
        imgLoader.DisplayImage(host + "/foto.ashx?login=" + alu.getLogin(), alumnoIcon);

        return row;
    }
	
}
